#include "media_sys.h"


bool Game(MEDIA_POI mp)//游戏总接口
{
    printf("启动游戏成功！\n");

    if(Show_Ui(mp,"/ui/game_ui.bmp") == false)
    {
        printf("显示游戏界面失败！\n");
        return  false;
    }


    if(Game_Touch_Ctrl(mp) == false)
    {
        printf("游戏触摸屏控制失败！\n");
        return false;
    }
    
    return true;
}


bool Game_Touch_Ctrl(MEDIA_POI mp)//游戏界面触摸控制
{
        //申请游戏的结构体堆空间
    P_GAME_I gi = (P_GAME_I)malloc(sizeof(GAME_I));
    if(gi == (P_GAME_I)NULL)
    {
        perror("malloc ...");
        return (gi)-1;
    }

    memset(gi,0,sizeof(GAME_I));
    
    gi->mp = mp;
    while(1)
    {
        //先获取点击的触摸坐标，再去判断坐标是哪个按钮的区间
        if(Get_Touch_Xy(mp,0) == false)
        {
            printf("读取触摸屏坐标失败！\n");
            return false;
        }

        if(mp->touch_x > 565 && mp->touch_x < 800 && mp->touch_y > 112 && mp->touch_y < 198) //开始
        {

            printf("游戏开始!\n");
            Game_Start(gi);
            if(Show_Ui(gi->mp,"/ui/game_ui.bmp") == false)
            {
                printf("显示主界面失败！\n");
                return false;
            }
        }

        if(mp->touch_x > 565 && mp->touch_x < 800 && mp->touch_y > 367 && mp->touch_y < 480) //退出
        {
            //显示主界面
            if(Show_Ui(mp,"/ui/main_ui.bmp") == false)
            {
                printf("显示主界面失败！\n");
                return false;
            }
            time_up_down = 1;
            mp->touch_x = -1;
            mp->touch_y = -1;

            free(gi);
            
            break;
        }
    }
    return true;

}


bool Game_Start(P_GAME_I gi)//游戏开始
{
    pthread_t id_3,id_4;

    gi->ball_switch = 99;
    if(pthread_create(&id_3,NULL,pthread_ball,gi) != 0)
    {
        perror("pthread_cread_create ...");
        return -1;
    }

    if(pthread_create(&id_4,NULL,pthread_slider,gi) != 0)
    {
        perror("pthread_cread_create ...");
        return -1;
    }

    pthread_join(id_3, NULL);
    pthread_join(id_4, NULL);
    
    return true;
}


bool move_ball(P_GAME_I gi)//球的移动
{
    int r = 50;
    int x0=400;
    int y0=240;
    int ball_color =0x000000;
    int fraction = 0;
    char fraction_c[256] = {0};
    sprintf(fraction_c,"%d",fraction);
    Sentence_Show_50(fraction_c,gi->mp,600,20);

    int x_mask = 1;
    int y_mask = 0; 
    while(1)
    {
        if (gi->ball_switch != 0)
        {
            for(int y=(y0-r-2);y<(y0+r+2);y++)
            {
                for(int x=(x0-r-2);x<(x0+r+2);x++)
                {
                    if((x-x0)*(x-x0)+(y-y0)*(y-y0)<= r * r)
                    {
                        *(gi->mp->mmap_start_poi +800*y+x)= ball_color;
                        ball_color++;
                    }
                    else
                    {
                        *(gi->mp->mmap_start_poi +800*y+x)= 0xffffff;
                    }
                }
            }
            usleep(500);
            if(y0 + r == 385)//转向
            {
                y_mask = 0;
                if (x0 < (gi->slider_x - gi->slider_size/2) || x0 > (gi->slider_x + gi->slider_size/2))
                {
                    gi->game_over = 0;
                    gi->ball_switch == 0;
                    printf("游戏结束ball\n");
                    Show_Ui(gi->mp,"/ui/game_over_ui.bmp");

                    int tmp_x = gi->mp->touch_x;
                    int tmp_y = gi->mp->touch_y;
                    while(1)
                    {
                        if(Get_Touch_Xy(gi->mp,0) == false)
                        {
                            printf("读取触摸屏坐标失败！\n");
                            return false;
                        }
                        if (tmp_x != gi->mp->touch_x || tmp_y != gi->mp->touch_y)
                        {
                            break;
                        }
                        
                    }

                    gi->mp->touch_x = -1;
                    gi->mp->touch_y = -1;
                    break;
                }
                else
                {
                    fraction += 100;
                    gi->slider_size -=10;
                    sprintf(fraction_c,"%d",fraction);
                    Sentence_Show_50(fraction_c,gi->mp,600,20);
                }
            }
            if(x0 +r == 560)x_mask = 0;
            if(y0 -r == 2)  y_mask = 1;
            if(x0 -r == 2)  x_mask = 1;

            if(y_mask == 0)y0--;
            if(y_mask == 1)y0++;
            if(x_mask == 0)x0--;
            if(x_mask == 1)x0++;
        }

        if(gi->ball_switch == 0) 
        {
            break;
        }

        while(gi->ball_switch == 2);
    }
    return true;

}


void *pthread_ball(void *arg)//球的移动多线程
{
    P_GAME_I gi = (P_GAME_I) arg;

    if(move_ball(gi) == false)
    {
        printf("球移动失败！\n");
        return (void *)-1;
    }
    return NULL;
}


bool move_slider(P_GAME_I gi)//滑块的移动
{
    gi->slider_size = 160;//滑块大小
    gi->mp->touch_x = 300;
    gi->slider_x    = 300;
    gi->mp->touch_y = 420;

    gi->game_over = 1;
    while(1)
    {
        if (gi->game_over == 0)
        {
            gi->ball_switch = 0;
            printf("游戏结束slider\n");
            Show_Ui(gi->mp,"/ui/game_over_ui.bmp");
            gi->mp->touch_x = -1;
            gi->mp->touch_y = -1;
            while(1)
            {
                if(Get_Touch_Xy(gi->mp,0) == false)
                {
                    printf("读取触摸屏坐标失败！\n");
                    return false;
                }

                if(gi->mp->touch_x > 260 && gi->mp->touch_x < 550 && gi->mp->touch_y > 290 && gi->mp->touch_y < 390)
                {
                    gi->mp->touch_x = -1;
                    gi->mp->touch_y = -1;
                    break;
                }
            }
            gi->mp->touch_x = -1;
            gi->mp->touch_y = -1;
            break;
        }
        
        //先获取点击的触摸坐标，再去判断滑块
        if(Get_Touch_Xy(gi->mp,1) == false)
        {
            printf("读取触摸屏坐标失败！\n");
            return false;
        }

        //滑块的滑动，显示
        if(gi->mp->touch_x > gi->slider_size/2 && gi->mp->touch_x < 560-gi->slider_size/2 && gi->mp->touch_y > 386 && gi->mp->touch_y < 445)
        {
            gi->ball_switch = 1;
            gi->slider_x = gi->mp->touch_x;
            for(int y=386;y<445;y++)
            {
                for(int x=0; x<560 ;x++)
                {
                    if(x>(gi->mp->touch_x-gi->slider_size/2) && x<(gi->mp->touch_x+gi->slider_size/2))
                    {
                        *(gi->mp->mmap_start_poi +800*y+x)= 0x000000;
                    }
                    else
                    {
                        *(gi->mp->mmap_start_poi +800*y+x)= 0xffffff;
                    }
                }
            }
        }

        //暂停
        if(gi->mp->touch_x > 565 && gi->mp->touch_x < 800 && gi->mp->touch_y > 189 && gi->mp->touch_y < 280) //暂停&继续
        {
            printf("暂停&继续 !\n");
            gi->ball_switch = 2;//暂停

            while(1)
            {
                if(Get_Touch_Xy(gi->mp,0) == false)
                {
                    printf("读取触摸屏坐标失败！\n");
                    return false;
                }

                if(gi->mp->touch_x > 565 && gi->mp->touch_x < 800 && gi->mp->touch_y > 210 && gi->mp->touch_y < 300) 
                {
                    gi->ball_switch = 1;
                    gi->mp->touch_x = -1;
                    gi->mp->touch_y = -1;
                    break;
                }
            }
        }
        
        //退出按钮//重新开始
        if((gi->mp->touch_x > 565 && gi->mp->touch_x < 800 && gi->mp->touch_y > 390 && gi->mp->touch_y < 480)||
            (gi->mp->touch_x > 565 && gi->mp->touch_x < 800 && gi->mp->touch_y > 300 && gi->mp->touch_y < 390))
        {
            gi->ball_switch = 0;

            if(Show_Ui(gi->mp,"/ui/game_ui.bmp") == false)
            {
                printf("显示主界面失败！\n");
                return false;
            }

            gi->mp->touch_x = -1;
            gi->mp->touch_y = -1;
            break;
        }
    }
    return true;
}   


void *pthread_slider(void *arg)//滑块的移动多线程
{
    P_GAME_I gi = (P_GAME_I) arg;

    if(move_slider(gi) == false)
    {
        printf("主界面触摸屏控制失败！\n");
        return (void *)-1;
    }
    return NULL;
}